package com.ymatou.iapi.usersafe.testcase;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.login.parameter.LoginAuthBean;
import com.ymatou.iapi.login.service.LoginAuthCall;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymatou.iapi.usersafe.parameter.DeviceAuthManageRequestDtoBean;
import com.ymatou.iapi.usersafe.service.DeviceAuthManageRequestDtoCall;
import com.ymatou.iapi.userservice.parameter.AddressRequestDtoBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AccountCallService;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.JUserSafe;
import com.ymttest.database.sqlwapper.UserSafeWapper;
import com.ymttest.utils.TestDataManager;
/**
*更新用户的所有IP区域认证状态为已认证
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_DeviceAuthManageRequestDto{
	private static DeviceAuthManageRequestDtoBean DeviceAuthManageRequestDtoBean;
	private static DeviceAuthManageRequestDtoCall DeviceAuthManageRequestDtoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新用户的所有IP区域认证状态为已认证");
	}

	@Before
	public void caseUp() {
		DeviceAuthManageRequestDtoBean=new DeviceAuthManageRequestDtoBean();
		DeviceAuthManageRequestDtoCall=new DeviceAuthManageRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(DeviceAuthManageRequestDtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeviceAuthManageRequestDto_001() {
		Logger.start(true,"更新买手用户的所有IP区域认证状态为已认证成功");
		try {
			JUserSafe.initConfig(2);
			UserRegisterInfoRequestDtoBean urb=JUserCallService.initUserRegisterInfoRequestDtoBean();
//			urb.setDeviceId(TestDataManager.getRandomUUID().toString());
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer uid=uc.getUserid();
			// 添加地址
						AddressRequestDtoBean addressBean = JUserCallService.initAddressBean(uid);
						JUserCallService.AddressRequestDtoCall(addressBean);
						// 设置账户余额
						String aid = AccountCallService.getAccountId(uid);
						AccountCallService.resetAccount(aid, new BigDecimal("1000"), new BigDecimal("100"));
						// 下单
						PlaceOrderService.payOrder(uid);
						
						String did=TestDataManager.getRandomUUID().toString();
						LoginAuthBean loginBean=JUserCallService.initLoginAuthBean(urb);
						loginBean.setLoginId(urb.getLoginId());
						loginBean.setDeviceId(did);
						loginBean.setClientIp(urb.getClientIp());
						JUserCallService.loginAuth(loginBean);
						
						Thread.sleep(3000);
			//使用不同的deviceid
			did=TestDataManager.getRandomUUID().toString();
			JUserCallService.login(urb.getLoginId(), urb.getPas(), "app", urb.getClientIp(), did);
			List<Map<String, Object>> mapo = new UserSafeWapper().getDeviceAuthByUserIdAndDeviceId(uid, did);
			Logger.debug(mapo);
			Thread.sleep(1000);
			String oldauthTime="";
			if(mapo!=null&&mapo.size()>0){
				oldauthTime=mapo.get(0).get("AuthTime").toString();
			}
			DeviceAuthManageRequestDtoBean.setUserId(uid);
			DeviceAuthManageRequestDtoCall.setData(DeviceAuthManageRequestDtoBean);
			DeviceAuthManageRequestDtoCall.callService();			
			Logger.verifyEquals("true", DeviceAuthManageRequestDtoCall.getString("Success"), "验证Success");
			
			List<Map<String, Object>> list = new UserSafeWapper().getDeviceAuthByUserIdAndDeviceId(uid, did);
			Logger.debug(list);
			for (Map<String, Object> map : list) {
					Logger.verifyEquals(200, map.get("AuthStatus"), "验证AuthStatus");
					Logger.verifyNotEquals(oldauthTime, map.get("AuthTime").toString(), "验证AuthTime");
			}
			LoginAuthCall loginc= new JUserCallService().login(urb.getLoginId(), urb.getPas(), "app", urb.getClientIp(), did);
			Logger.verifyEquals(true, loginc.getSuccess(), "验证Success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_DeviceAuthManageRequestDto_002() {
		Logger.start(false,"缺少参数UserId,失败");
		try {
			DeviceAuthManageRequestDtoCall.setData(DeviceAuthManageRequestDtoBean);
			DeviceAuthManageRequestDtoCall.callService();			
			Logger.verifyIsContains("非法的用户ID", DeviceAuthManageRequestDtoCall.getReturnData(), "验证返回信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeviceAuthManageRequestDto_003() {
		Logger.start(true,"更新买手用户的所有IP区域认证状态为已认证成功");
		try {
			JUserSafe.initConfig(2);
			UserRegisterInfoRequestDtoBean urb=JUserCallService.initUserRegisterInfoRequestDtoBean();
			urb.setDeviceId(TestDataManager.getRandomUUID().toString());
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userid=uc.getUserid();
			
			// 添加地址
			AddressRequestDtoBean addressBean = JUserCallService.initAddressBean(userid);
			JUserCallService.AddressRequestDtoCall(addressBean);
			// 设置账户余额
			String aid = AccountCallService.getAccountId(userid);
			AccountCallService.resetAccount(aid, new BigDecimal("1000"), new BigDecimal("100"));
			// 下单
			PlaceOrderService.payOrder(userid);
			
			String did=TestDataManager.getRandomUUID().toString();
			LoginAuthBean loginBean=JUserCallService.initLoginAuthBean(urb);
			loginBean.setLoginId(urb.getLoginId());
			loginBean.setDeviceId(did);
			loginBean.setClientIp(urb.getClientIp());
			JUserCallService.loginAuth(loginBean);
			
			Thread.sleep(3000);
			
			
			did=TestDataManager.getRandomUUID().toString();
			JUserCallService.login(urb.getLoginId(), urb.getPas(), "app", urb.getClientIp(), did);
			List<Map<String, Object>> mapo = new UserSafeWapper().getDeviceAuthByUserIdAndDeviceId(userid, did);
			Logger.debug(mapo);
			Thread.sleep(1000);
			String oldauthTime="";
			if(mapo!=null&&mapo.size()>0){
				oldauthTime=mapo.get(0).get("AuthTime").toString();
			}
			DeviceAuthManageRequestDtoBean.setUserId(userid);
			DeviceAuthManageRequestDtoCall.setData(DeviceAuthManageRequestDtoBean);
			DeviceAuthManageRequestDtoCall.callService();			
			Logger.verifyEquals("true", DeviceAuthManageRequestDtoCall.getString("Success"), "验证Success");
			
			List<Map<String, Object>> list = new UserSafeWapper().getDeviceAuthByUserIdAndDeviceId(userid, did);
			for (Map<String, Object> map : list) {
					Logger.verifyEquals(200, map.get("AuthStatus"), "验证AuthStatus");
					Logger.verifyNotEquals(oldauthTime, map.get("AuthTime").toString(), "验证AuthTime");
			}
			LoginAuthCall loginc= new JUserCallService().login(urb.getLoginId(), urb.getPas(), "app", urb.getClientIp(), urb.getDeviceId());
			Logger.verifyEquals(true, loginc.getSuccess(), "验证Success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}